import json
import datetime
from django.http import HttpResponse
from app.models import *
from tools.get_now_time import get_now_time
from tools.log import log
pass_salt = "username_salt"


# 实现更新角色信息的业务处理
def updateRoleInformation(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
    # 获取唯一标识符，此处为role
    role = data.get('role', None)
    right_level = data.get('right_level',None)
    # 进行数据用户数据更新
    context = dict()
    try:
        Role.objects.filter(role=role).update(**data)
        User.objects.filter(position=role).update(right_level=right_level)
        #写入log日志
        cookie=request.get_signed_cookie("account",salt=pass_salt)
        type = "编辑表格数据"
        time = get_now_time()
        affair = cookie+"更新了角色信息"
        log(type, time, affair)
        context['result'] = 1
    except:
        context['result'] = 0
    return context

# 实现更新用户信息的业务处理
def updateUserInformation(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
    # 获取唯一标识符，此处为email
    email = data.get('email', None)
    # 进行数据用户数据更新
    context = dict()
    try:
        User.objects.filter(email=email).update(**data)
        #写入log日志
        cookie=request.get_signed_cookie("account",salt=pass_salt)
        type = "编辑表格数据"
        time = get_now_time()
        affair = cookie+"更新了用户信息"
        log(type, time, affair)
        context['result'] = 1
        print(1)
    except:
        context['result'] = 0
        print(0)
    return context

# 实现更新仓库信息的业务处理
def updateWarehouseInformation(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
    # 获取唯一标识符，此处为id
    code = data.get('code', None)
    # 进行数据用户数据更新
    context = dict()
    try:
        Warehouse.objects.filter(code=code).update(**data)
        #写入log日志
        cookie=request.get_signed_cookie("account",salt=pass_salt)
        type = "编辑表格数据"
        time = get_now_time()
        affair = cookie+"更新了仓库信息"
        log(type, time, affair)
        context['result'] = 1
    except:
        context['result'] = 0
    return context

# 实现更新物料信息的业务处理
def updateMaterialInformation(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
    # 将日期字符串转化为Datetime
    # 暂不修改先不写了
    # 获取唯一标识符，此处为id
    code = data.get('code', None)
    # 进行数据用户数据更新
    context = dict()
    try:
        Material.objects.filter(code=code).update(**data)
        #写入log日志
        cookie=request.get_signed_cookie("account",salt=pass_salt)
        type = "编辑表格数据"
        time = get_now_time()
        affair = cookie+"更新了物料信息"
        log(type, time, affair)
        context['result'] = 1
    except:
        context['result'] = 0
    return context

# 实现更新部门信息的业务处理
def updateDepartmentInformation(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
    # 获取唯一标识符，此处为position
    name = data.get('name', None)
    # 进行数据用户数据更新
    context = dict()
    try:
        Department.objects.filter(name=name).update(**data)
        #写入log日志
        cookie=request.get_signed_cookie("account",salt=pass_salt)
        type = "编辑表格数据"
        time = get_now_time()
        affair = cookie+"更新了部门信息"
        log(type, time, affair)
        print(1)
        context['result'] = 1
    except:
        print(0)
        context['result'] = 0
    return context

# 实现更新物料类别信息的业务处理
def updateMaterialTypeInformation(request):
    # 获取用户请求数据
    req = json.loads(request.body)
    # 获取数据
    data = dict(req.get('data', None))
    # 消去id
    data.pop('id')
    # 获取唯一标识符，此处为role
    type_name = data.get('type_name', None)
    # 进行数据用户数据更新
    context = dict()
    try:
        MaterialType.objects.filter(type_name=type_name).update(**data)
        #写入log日志
        cookie=request.get_signed_cookie("account",salt=pass_salt)
        type = "编辑表格数据"
        time = get_now_time()
        affair = cookie+"更新了物料类型信息"
        log(type, time, affair)

        context['result'] = 1
    except:
        context['result'] = 0
    return context

